package com.molichuxing.gateway.bff.financial.convertor;

import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.NumberToCNUtils;
import com.molichuxing.gateway.bff.financial.entity.response.ContractRenegeParamsVo;
import com.molichuxing.gateway.bff.financial.entity.response.ContractVo;
import com.molichuxing.gateway.bff.financial.entity.response.InvoiceVo;
import com.molichuxing.services.business.dto.response.UserBizDto;
import com.molichuxing.services.infrastructure.dto.request.modify.InvoiceDto;
import com.molichuxing.services.infrastructure.dto.response.ContractDto;
import com.molichuxing.services.infrastructure.dto.response.OrderRenegeDto;
import com.molichuxing.services.infrastructure.dto.response.SubcompanyDto;
import com.molichuxing.services.property.InvoiceBusinessTypeEnum;
import com.molichuxing.services.property.InvoiceTypeEnum;
import com.molichuxing.services.property.OrderRenegeCostTypeEnum;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class Convert {

    /*********************************财务转换开始*************************************************/

    public static List<InvoiceVo> toInvoiceVoList(List<InvoiceDto> dtoList) {
        List<InvoiceVo> voList = new ArrayList<>();
        if (dtoList != null && dtoList.size() > 0) {
            for (InvoiceDto invoiceDto : dtoList) {
                voList.add(toInvoiceVo(invoiceDto));
            }
        }
        return voList;
    }

    public static InvoiceVo toInvoiceVo(InvoiceDto invoiceDto) {
        InvoiceVo invoiceVo = new InvoiceVo();
        if (null == invoiceDto) {
            return invoiceVo;
        }

        BeanUtils.copyProperties(invoiceDto, invoiceVo);
        Integer businessType = invoiceDto.getBusinessType();
        String businessTypeStr = invoiceDto.getBusinessTypeStr();
        Integer currentPeriods = invoiceDto.getCurrentPeriods();
        Integer type = invoiceDto.getType();
        String typeStr = invoiceDto.getTypeStr();
        StringBuilder invoiceType = new StringBuilder();
        if (businessType != null && InvoiceBusinessTypeEnum.EXCLUSIVEL.getValue() == businessType) {
            invoiceType.append(businessTypeStr).append("-").append(currentPeriods).append("期").append("-").append("用车费");
        } else if (businessType != null && InvoiceBusinessTypeEnum.SUBSCRIBE.getValue() == businessType) {
            invoiceType.append(businessTypeStr);
            if (InvoiceTypeEnum.MAIN_ORDER.getValue() != type) {
                invoiceType.append("-").append(typeStr);
            }
        }

        invoiceVo.setInvoiceType(String.valueOf(invoiceType));
        LocalDateTime validityStartTime = invoiceDto.getValidityStartTime();
        LocalDateTime validityEndTime = invoiceDto.getValidityEndTime();
        invoiceVo.setValidityStartTime(validityStartTime.toLocalDate());
        invoiceVo.setValidityEndTime(validityEndTime.toLocalDate());
        LocalDateTime now = LocalDateTime.now();
        if (now.isAfter(validityStartTime) && now.isBefore(validityEndTime)) {
            invoiceVo.setIsInvoice(1);
        } else {
            invoiceVo.setIsInvoice(0);
        }

        return invoiceVo;
    }

    public static List<ContractVo> toContractVoList(List<ContractDto> dtoList) {
        List<ContractVo> voList = new ArrayList<>();
        if (dtoList != null && dtoList.size() > 0) {
            for (ContractDto ContractDto : dtoList) {
                voList.add(toContractVo(ContractDto));
            }
        }
        return voList;
    }

    public static ContractVo toContractVo(ContractDto contractDto) {
        ContractVo contractVo = new ContractVo();
        if (null == contractVo) {
            return contractVo;
        }

        BeanUtils.copyProperties(contractDto, contractVo);
        return contractVo;
    }


    public static ContractRenegeParamsVo toContractRenegeParamsVo(ContractRenegeParamsVo vo, OrderRenegeDto dto) {
        if (dto == null) {
            return vo;
        }
        vo = vo == null ? new ContractRenegeParamsVo() : vo;

        if (OrderRenegeCostTypeEnum.INCOME.equals(dto.getCostType())) {
            vo.setCostType("2.1");
            vo.setIncom(dto.getCostAmount().toPlainString());
            vo.setIncomUpper(NumberToCNUtils.number2CNMontrayUnit(dto.getCostAmount()));
            vo.setDisburse("/");
            vo.setDisburseUpper("/");
        } else if (OrderRenegeCostTypeEnum.DISBURSE.equals(dto.getCostType())) {
            vo.setCostType("2.2");
            vo.setIncom("/");
            vo.setIncomUpper("/");
            vo.setDisburse(dto.getCostAmount().toPlainString());
            vo.setDisburseUpper(NumberToCNUtils.number2CNMontrayUnit(dto.getCostAmount()));
        }
        return vo;
    }

    public static ContractRenegeParamsVo toContractRenegeParamsVo(ContractRenegeParamsVo vo, UserBizDto dto) {
        if (dto == null) {
            return vo;
        }
        vo = vo == null ? new ContractRenegeParamsVo() : vo;

        vo.setPartyBId(dto.getId());
        vo.setPartyBName(dto.getRealName());
        vo.setPartyBCard(dto.getIdNumber());
        vo.setPartyBAddress(dto.getAddress());
        vo.setPartyBPhone(dto.getPhone());

        return vo;
    }

    public static ContractRenegeParamsVo toContractRenegeParamsVo(ContractRenegeParamsVo vo, ContractDto dto) {
        if (dto == null) {
            return vo;
        }
        vo = vo == null ? new ContractRenegeParamsVo() : vo;

        vo.setMainContractNumber(dto.getContractCode());
        vo.setMainDate(DateUtil.formatDate(dto.getSignedTime(), "yyyy年MM月dd日"));

        return vo;
    }

    public static ContractRenegeParamsVo toContractRenegeParamsVo(ContractRenegeParamsVo vo, SubcompanyDto dto) {
        if (dto == null) {
            return vo;
        }
        vo = vo == null ? new ContractRenegeParamsVo() : vo;

        vo.setPartyAId(dto.getId());
        vo.setPartyAName(dto.getFullName());
        if ("2.1".equals(vo.getCostType())) {
            vo.setPartyAName2(dto.getFullName());
            vo.setPartyABankAccount(dto.getBankNumber());
            vo.setPartyABank(dto.getAccountBank());
        } else {
            vo.setPartyAName2("/");
            vo.setPartyABankAccount("/");
            vo.setPartyABank("/");
        }

        return vo;
    }
}
